package com.ndp.fb.walle.controller;

import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.model.business.vo.ectemplate.EcElementFilterParam;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.rdb.model.ectemplate.EcProductLogo;
import com.ndp.fb.walle.business.EcProductLogoService;
import com.ndp.fb.walle.model.bo.ectemplate.EcProductLogoResult;
import com.ndp.fb.walle.model.bo.ectemplate.ParseElementResult;
import com.ndp.fb.walle.model.vo.PageResult;
import com.ndp.fb.walle.util.ZipUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by sisi.chen on 2016/7/5.
 */
@RestController
@RequestMapping("/ecProductLogo")
public class EcProductLogoController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private EcProductLogoService ecProductLogoService;

    /**
     * 品牌上传
     */
    @RequestMapping("upload")
    public List<ParseElementResult> upload(HttpServletRequest request) throws Exception {
        logger.info("EcProductLogolController upload() begin.");
        User user = (User) request.getSession().getAttribute("user");
        MultipartHttpServletRequest mR = (MultipartHttpServletRequest)request;
        // 获得上传文件列表
        List<MultipartFile> fileList = mR.getFiles("files");
        String templateId = request.getParameter("templateId");
        String productId = request.getParameter("productId");
        return ecProductLogoService.upload(null == templateId ? null : Long.valueOf((String)templateId), Long.valueOf(productId), fileList, request.getRealPath("/"), user);
    }

    /**
     * 根据模板或全量logo
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("list")
    public Map list(@RequestBody EcElementFilterParam param, HttpServletRequest request) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        Integer sizeNo = param.getPSize() == null || param.getPSize() < 1 ? CommonConstants.DEFAULT_PAGE_SIZE_5 : param.getPSize();
        Integer firstResult = param.getCPage() == null  || param.getCPage() < 1? 0 : (param.getCPage() - 1) * sizeNo;
        Pager<EcProductLogoResult>  results = ecProductLogoService.list(user.getOrgId(), param.getTemplateId(), param.getColor(), firstResult, sizeNo);
        Long tSize = results.getTotalSize() == null ? 0 : results.getTotalSize();
        float nrOfPages = (float) tSize / sizeNo;
        int tPage = (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
        PageResult pageResult = new PageResult(tPage, param.getCPage(), sizeNo, tSize);

        // 封装响应结果
        Map data = new HashMap<>();
        data.put("productLogos", results.getResultList());
        data.put("paginator", pageResult);

        return data;
    }

    /**
     * 根据模板或全量logo
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("filter")
    public Map<String, EcProductLogo> filter(@RequestBody EcElementFilterParam param) throws Exception {
        return ecProductLogoService.filter(param);
    }
}
